package com.aidysoft.practice.easypoi.demo1.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;

import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.aidysoft.practice.easypoi.demo1.entity.CompanyHasImgModel;
import com.aidysoft.practice.easypoi.demo1.entity.CourseEntity;
import com.aidysoft.practice.easypoi.demo1.entity.StudentEntity;
import com.aidysoft.practice.easypoi.utils.pojo.wordExportImage.WordImageEntity;
import com.aidysoft.practice.easypoi.demo1.excelData.ExcelData;
import com.aidysoft.practice.easypoi.utils.ExcelUtil;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@RestController
public class ExcelController {

    @Autowired
    private ExcelUtil excelUtil;
    @Autowired
    private ExcelData excelData;

    /**
     * 单对象
     *
     * @param response
     */
    @RequestMapping("/easyExport")
    public void loadEasyExcel(HttpServletResponse response) {

        ExcelUtil.exportExcel
                (
                        excelData.getEasyList(),
                        "计算机一班",
                        "学生统计",
                        StudentEntity.class,
                        "学生统计.xlsx", response
                );

    }

    /**
     * 包含 对象集合
     *
     * @param response
     */
    @RequestMapping("/export")
    public void loadExcel(HttpServletResponse response) {

        ExcelUtil.exportExcel
                (
                        excelData.getList(),
                        "计算机一班",
                        "学生课程统计",
                        CourseEntity.class,
                        "学生课程统计.xls",
                        response
                );

    }

    /**
     * Excel 多Sheet导出 非模板
     *
     * @param response
     */
    @RequestMapping("/sheetExport")
    public void loadSheetExcel(HttpServletResponse response) {
        ExcelUtil.exportExcel
                (
                        excelData.getMoreSheetData(),
                        "计算机系学生.xls",
                        response
                );
    }

    /**
     * 模板 单sheet 到处
     *
     * @param response
     */
    @RequestMapping("/templateExport")
    public void loadTemplateExcel(HttpServletResponse response) {

        TemplateExportParams templateExportParams =
                new TemplateExportParams("templates/template.xlsx");

        templateExportParams.setHeadingStartRow(0);
        templateExportParams.setHeadingRows(2);
        //demo2.xlsx
        ExcelUtil.exportTemplateExcel
                (
                        excelData.getExcelDataByTemplate(),
                        templateExportParams,
                        "学生信息.xlsx",
                        response
                );
    }

    /**
     * 导入
     * 上传文件
     *
     * @param file
     */
    @RequestMapping("/importExcelFile")
    public void importExcel(@RequestParam(value = "excelFile") MultipartFile file) {

        //解析excel，
        List<StudentEntity> personList = excelUtil.importExcel(file, 1, 1, StudentEntity.class);

        for (StudentEntity studentEntity : personList) {
            System.out.println(studentEntity.toString());
        }
        System.out.println("导入数据一共【" + personList.size() + "】行");

    }

    /**
     * 导入
     * <p>
     * 本地文件路径
     */
@RequestMapping("/importExcel")
    public void importExcelMap() {

        String filePath = "D:\\学生统计.xlsx";
        //解析excel，
        List<Map<String,Object>> personList = excelUtil.importExcel(filePath, 1, 1, Map.class);
        for (Map<String,Object> studentEntity : personList) {
            System.out.println(studentEntity.values());
        }
        System.out.println("导入数据一共【" + personList.size() + "】行");
    }


    /**
     *  图片
     *  导出
     *
     */
    @RequestMapping("/exportPic")
    @ResponseBody
    public void exportPic(HttpServletResponse response) {

        List<CompanyHasImgModel> list = new ArrayList<CompanyHasImgModel>();

        list.add(new CompanyHasImgModel("百度", "templates/imgs/1.jpg", "北京市海淀区西北旺东路10号院百度科技园1号楼"));
        list.add(new CompanyHasImgModel("阿里巴巴", "templates/imgs/2.jpg", "北京市海淀区西北旺东路10号院百度科技园1号楼"));
        list.add(new CompanyHasImgModel("Lemur", "templates/imgs/3.jpg", "亚马逊热带雨林"));
        list.add(new CompanyHasImgModel("一众", "templates/imgs/4.jpg", "山东济宁俺家"));

        //导出操作
        ExcelUtil.exportExcel(list, "公司信息", "公司表", CompanyHasImgModel.class, "公司表.xls", response);

    }

    /**
     * 大数据量导出
     *
     */
    @RequestMapping(value = "/bigDataExport")
    public void bigDataExport(HttpServletResponse response){

        List<StudentEntity> list = new ArrayList<>();
        Workbook workbook = null;
        Date start = new Date();
        ExportParams params = new ExportParams("大数据测试", "测试");
        for (int i = 0; i < 3000000; i++) {  //一百万数据量
            //10万   6秒左右   100万  60-70秒
            StudentEntity person = new StudentEntity();
            person.setBirthday(new Date());
            person.setName("小明" + i);
            person.setSex(1);
            list.add(person);
            int num=0;
            if (list.size() == 100000) {
                num=num+1;
                workbook = ExcelExportUtil.exportBigExcel(params, StudentEntity.class, list);
                System.out.println(num);
                list.clear();
            }
        }

        ExcelExportUtil.closeExportBigExcel();
        System.out.println("时间用时：=" + (new Date().getTime() - start.getTime()));
        ExcelUtil.downLoadExcel("大数据.xlsx",response,workbook);
    }



    @GetMapping("/exportByMap")
    public String multiSheetExportByMapUtil(HttpServletResponse response) {

        List<ExcelExportEntity> entityList = new ArrayList<ExcelExportEntity>();

        entityList.add(new ExcelExportEntity("姓名", "name"));
        entityList.add(new ExcelExportEntity("年龄", "age"));
        entityList.add(new ExcelExportEntity("爱好", "hobby"));

        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

        for (int i = 0; i < 200; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", "1" + i);
            map.put("sex", "2" + i);
            map.put("age", "2" + i);
            map.put("hobby", "2" + i);

            data.add(map);
        }

/*        List<Map<String, Object>> list = Lists.newArrayList();

        Map<String, Object> map= Maps.newHashMap();
        map.put("title",new ExportParams("测试","测试"));
        map.put("entity",entityList);
        map.put("data",data);

         Map<String, Object> map1= Maps.newHashMap();
        map1.put("title",new ExportParams("测试1","测试1"));
        map1.put("entity",entityList);
        map1.put("data",data);

        list.add(map);
        list.add(map1);*/

          Workbook  workbook = ExcelUtil.exportExcel(new ExportParams("测试","测试"),entityList,data, ExcelType.HSSF);

        if (workbook == null) {
            return "faile";
        }
        ExcelUtil.downLoadExcel("map.xls", response, workbook);
        return "success";
    }

}
